package com.lixh.virtual.ui.appList.presenter;


import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;

import com.lixh.presenter.BasePresenter;
import com.lody.virtual.helper.utils.Reflect;
import com.lixh.virtual.IExtra;
import com.lixh.virtual.app.os.AppRepository;
import com.lixh.virtual.ui.appList.fragment.ListAppFragment;

;import java.io.File;


/**
 * Created by LIXH on 2016/12/21.
 * email lixhVip9@163.com
 * des
 */
public class AppListPresenter extends BasePresenter<ListAppFragment> {

    File file;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StorageManager storage = (StorageManager) view.activity.getSystemService(Context.STORAGE_SERVICE);
            for (StorageVolume volume : storage.getStorageVolumes()) {
                //Why the fuck are getPathFile and getUserLabel hidden?!
                //StorageVolume is kinda useless without those...
                File dir = Reflect.on(volume).call("getPathFile").get();
                if (dir.listFiles() != null) {
                    file = dir;
                }
            }
        } else {
            // Fallback: only support the default storage sources
            File storageFir = Environment.getExternalStorageDirectory();
            if (storageFir != null && storageFir.isDirectory()) {
                file = storageFir;
            }
        }
    }

    public void getInstalledApps(int type) {
        if (type == IExtra.APP_CLONE_INSTANCE) {
            AppRepository.get().getInstalledApps(view.activity).done(view::setAppData);
        } else {
            AppRepository.get().getStorageApps(view.activity, file).done(view::setAppData);
        }
    }
}
